package com.dream.xiaobo.controller;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xiaobo
 * @date 2021/8/30 - 15:13
 */
@WebServlet("/code")
public class CodeController extends BaseServlet {

    /**
     * 生成校验码
     * @param request
     * @param response
     * @throws IOException
     */
    public void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //生成验证码对象
        ValidateCode validateCode = new ValidateCode(100,35,5,20);
        //生成验证码
        String code = validateCode.getCode();
        //将验证码存放到Session中、返回给页面
        request.getSession().setAttribute("code",code);
        //创建返回对象
        ServletOutputStream outputStream = response.getOutputStream();
        //返回
        validateCode.write(outputStream);
    }

}
